<?php
declare (strict_types = 1);

namespace app\controller;

/**
 * 后台控制器 总继承初始
 * by 韩宇 qq571031767
 */

use app\common\model\Iplog;
use app\common\model\Menu;
use app\common\model\ThemeAdmin;
use app\common\model\UserLevel;
use think\facade\Request;
use think\facade\Session;
use think\facade\View;

class AdminInit extends Common
{
    public function __construct()
    {
        parent::__construct();

        //无权限时候显示
        $error_str = file_get_contents(app()->getRootPath().'view/public/no_power_str.html');
        //无菜单时候显示
        $no_menu_str = file_get_contents(app()->getRootPath().'view/public/no_menu_str.html');

        $user = $this->user;
        if(!$user){
            return redirect((string) url('Index/index',['redirect' => 'index/user/index']));
        }


        $user_level = UserLevel::where("id",$user["level_id"])->find()->toArray();
       if($user_level["power"]){
           $power = json_decode($user_level["power"],true);
       }else{
           $power = [];
       }

       //判断当前访问的菜单是否有权限
       $controller = \request()->controller();
       $action = \request()->action();
       $map["module_name"] = $controller;
       $map["action_name"] = $action;

       //超管无需验证权限
       //if(!$user["username"] == 'admin'){
       //    $current_menu = Menu::where($map)->find();
       //    if(!$current_menu){
       //        if(Request::isAjax()){
       //            return json(["code"=>40001,"msg"=>"无权限"]);
       //        }else{
       //            echo $error_str;die;
       //        }
       //    }
       //}


        $current_menu = Menu::where($map)->find();
        if(!$current_menu){
            if(Request::isAjax()){
                return json(["code"=>40001,"msg"=>"无此菜单"]);
            }else{
                p($map);
                echo $no_menu_str;die;
            }
        }

        //没有权限
        if(!in_array($current_menu["id"],$power)){
            if(Request::isAjax()){
                return json(["code"=>40001,"msg"=>"无权限"]);
            }else{
                p($map);
                echo $error_str;die;
            }
        }


        $menu = cache("menu");
        if(!$menu){
            $menu  = Menu::where("id","in",$power)
                ->where("display",1)
                ->order("ordid Asc")
                ->select()->toArray();
            cache("menu",$menu);
        }

        if(!$menu){
            $menu = [];
        }
        if(empty($menu)){
            if(Request::isAjax()){
                return json(["code"=>40001,"msg"=>"无此菜单，请先添加"]);
            }else{
                p("无权限");die;
            }
        }
        foreach ($menu as $k => $v){
            if($v["module_name"] && $v["action_name"]){
                $menu[$k]["url"] = url($v["module_name"]."/".$v["action_name"]);
            }else{
                $menu[$k]["url"] = "javascript:;";
            }

        }


        $menu = forztree($menu,"children");
        View::assign("menu",$menu);

        $today_pv = Iplog::whereTime('create_time','today')->count();
        View::assign("today_pv",$today_pv);

        $pv_count = Iplog::count();
        View::assign("pv_count",$pv_count);

        $user_count = \app\common\model\User::count();
        View::assign("user_count",$user_count);

        $today_comment = \app\common\model\Comment::whereTime('create_time','today')->count();
        View::assign("today_comment",$today_comment);
        $article_total = \app\common\model\Article::count();
        View::assign("article_total",$article_total);
        $comment_count = \app\common\model\Comment::count();
        View::assign("comment_count",$comment_count);

        //获取当前的url 保证菜单的选中效果
        $menu_pid = input("param.menu_pid","");
        $menu_id = input("param.menu_id","");
        if($menu_id && $menu_pid){
            Session::set("menu_id",$menu_id);
            Session::set("menu_pid",$menu_pid);
        }

    }
}
